#include <conio.h>
#include "../../HeadFiles/PagesH/CostumerViewCommoditiesPage.h"
#include "../../HeadFiles/StandardHeads.h"
#include "../../HeadFiles/InputCheck.h"
#include "../../HeadFiles/ChainsH/CartChain.h"

// 不同等级客户的折扣
static const double disCountStandard[] = {1.00, 0.97, 0.95, 0.93, 0.90, 0.87};

extern struct CommodityNode * commodityHead, * commodityTail;

void addThisToCart(struct CommodityNode * commodity, int count, int lv);

void displayThisCommodityForCustomer(struct CommodityNode * this, int lv)
{
    puts(SPLIT_STR);
    printf("Commodity Name: %s\n", this->commodityName);
    printf("Unit Price: %.2f\n", this->soldPrice * disCountStandard[lv]);
    if(this->unitCount > 0)
        printf("Surplus: %d\n", this->unitCount);
    else
        puts("OUT OF STOCK!");

}

int costumerViewCommodities(struct ClientNode * nowClient)
{
    struct CommodityNode * this = commodityHead;
    if (this == NULL)
    {
        puts("No commodity on sale!");
        system("pause");
        return 0;
    }
    Start:
    system("cls");
    puts("type 1 for the previous");
    puts("type 2 for the next");
    puts("type 3 to add this to the cart");
    puts("type 0 to get back");
    puts(SPLIT_STR);
    displayThisCommodityForCustomer(this, nowClient->lv);
    bool outOfStock = this->unitCount <= 0; //缺货状态
    if(outOfStock)
        puts("OUT OF STOCK!");
    puts(SPLIT_STR);
    char opt = getche();
    while (!(opt == '1' || opt == '2' || opt == '3' || opt == '0'))
    {
        puts("Illegal input detected! Try again: ");
        opt = getche();
    }
    if (opt == '1')
    {
        // 查找上一个
        if (this == commodityHead)
        {
            puts("No previous commodity in list!");
            system("pause");
            goto Start;
        }else{
            this = this->preCommodity;
        }
        system("cls");
        goto Start;
    }else if (opt == '2'){
        // 查找下一个
        if (this == commodityTail)
        {
            puts("No next commodity in list!");
            system("pause");
        }else{
            this = this->nextCommodity;
        }
        system("cls");
        goto Start;
    }else if (opt == '3'){
        // 加入购物车
        if(outOfStock)
        {
            // 已经没有库存的话，就不能再加入购物车
            puts("OUT OF STOCK!");
            system("pause");
            goto Start;
        }
        puts(SPLIT_STR);
        puts("Select a packaging method");
        puts("1 for dozen(12X)");
        puts("2 for case(24X)");
        puts("0 to abort");
        char opt2 = getche();
        while(!(opt2 == '1' || opt2 == '2' || opt2 == '0'))
        {
            puts("Illegal input detected! Try again: ");
            opt2 = getche();
        }
        if(opt2 == '1' || opt2 == '2')
        {
            char num[STR_MAX_LEN];
            puts("Input the number of unit");
            scanf("%s", num);
            while(!(isFullDigit(num) && strlen(num) <= 7))
            {
                puts("Illegal input detected! Try again: ");
                scanf("%s", num);
            }
            if(strToInt(num) > this->unitCount)
            {
                // 如果输入的数量大于库存就报错回去
                puts("OUT OF STOCK!");
                system("pause");
                goto Start;
            }
            int totalPrice = strToInt(num) * (opt2 == '1' ? 12 : 24);
            if(nowClient->balance < totalPrice)
            {
                puts("Insufficient funds! Please deposit first!");
                system("pause");
                goto Start;
            }
            else
                addThisToCart(this, strToInt(num) * (opt2 == '1' ? 12 : 24), nowClient->lv);
            goto Start;
        }else{
            goto Start;
        }
    }else{
        return 1;
    }
}


extern struct CartNode * cartHead, * cartTail;
void addThisToCart(struct CommodityNode * commodityPtr, int count, int lv)
{
    struct CartNode * now = (struct CartNode *)malloc(sizeof(struct CartNode));
    now->preCart = now->nextCart = NULL;

    if(cartHead == NULL)
    {
        // 添加第一件商品
        cartHead = cartTail = now;
        memcpy(now->unitName, commodityPtr->commodityName, sizeof(now->unitName));
        now->soldPrice_Original = commodityPtr->soldPrice;
        now->soldPrice_Discount = commodityPtr->soldPrice * disCountStandard[lv];
        now->unitCount = count;
        return ;
    }else{
        cartTail->nextCart = now;
        now->preCart = cartTail;
        cartTail = now;
        memcpy(now->unitName, commodityPtr->commodityName, sizeof(now->unitName));
        now->soldPrice_Original = commodityPtr->soldPrice;
        now->soldPrice_Discount = commodityPtr->soldPrice * disCountStandard[lv];
        now->unitCount = count;
        return ;
    }
}
